package com.qf.web;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Employee;
import com.qf.service.EmployeeService;
import com.qf.util.filter.enum1.PageEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 2019/11/19
 * Administrator
 * ssm1119
 * 面向对象面向君  不负代码不负卿
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    //新增用户
    @RequestMapping("/resource/demo2/insertEmp")
    public String  insert(Employee employee){
        employee.setCreatedate(new Date());
        employee.setState(1);//1表示未删除  0表示已删除
        int insert = employeeService.insert(employee);
        if(insert>0){
            //新增成功
            return "redirect:/resource/demo2/getusers";
        }
        return "redirect:/resource/demo2/getalldepts";
    }

    //用户管理模块的查询列表 index表示页码值
    @RequestMapping("/resource/demo2/getusers")
    public String getall(@RequestParam(defaultValue = "1") int index,
                         ModelMap map,String username,String rolename,String deptname){
        PageInfo<Employee> pageInfo = employeeService.getall(index, PageEnum.PAGESIZE.getSize(),username,rolename,deptname);
         map.addAttribute("pi",pageInfo);
         //存储模糊查条件到页面
         map.addAttribute("uname",username);
         map.addAttribute("rname",rolename);
         map.addAttribute("dname",deptname);
         //  /list.jsp
        return "resource/demo2/list";
    }

        //登录
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){
        Employee employee = employeeService.login(username, password);
        if(employee==null){
            return "login";
        }
        //将登录成功的信息保存在session中
        session.setAttribute("user",employee);
        return "redirect:/index.jsp";
    }
    //退出
    @RequestMapping("/loginout")
    public String loginout(HttpSession session){
            //清除session
        session.invalidate();
        return "redirect:/login.jsp";
    }

}
